import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { networkInterfaces } from 'os';
import { NestExpressApplication } from '@nestjs/platform-express';
import { static as resource } from 'express';
import * as chalk from 'chalk';
import * as nunjucks from 'nunjucks';

const PORT = 3000;
const ips = networkInterfaces();

function serverStartInfo(): string {
  let startInfo = '';
  Object.keys(ips).map((item) => {
    ips[item].forEach((net) => {
      if (net.family === 'IPv4') {
        const str = net.address === '127.0.0.1' ? '本地地址' : '网络地址';
        startInfo += `${str}：${chalk.default.cyan(`http://${net.address}:${PORT}`)}\n`;
      }
    });
  });
  return startInfo;
}

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  app.use('/static', resource('static')); // 设置静态文件目录
  nunjucks.configure('resource/views', {
    autoescape: true,
    express: app,
    watch: process.env.NODE_ENV !== 'production',
  });
  app.set('view options', {
    debug: process.env.NODE_ENV !== 'production',
  });
  app.setViewEngine('njk');
  await app.listen(PORT);
  console.log(serverStartInfo());
}
bootstrap();
